import os

import pytest

from interface_item.api.feishu_api import FeiShuApi
from interface_item.utils.logger import logger
from interface_item.utils.read_data import r_data

'''
用户组管理模块
'''

BASE_PATH = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))


class UserGroup(FeiShuApi):

    def create_group(self, name, group_id, description="", type_g=1):
        url = "/contact/v3/group"
        data = {
            "group_id": group_id,
            "name": name,
            "description": description,
            "type": type_g
        }
        res = self.fs_requests(url=url, method="post", json=data)
        logger.debug(f"请求成功，返回信息为：{res.json()}")
        return res

    def update_group(self, group_id, name, description):
        url = f"/contact/v3/group/{group_id}"
        data = {
            "name": name,
            "description": description
        }
        res = self.fs_requests(url=url, method="patch", json=data)
        logger.debug(f"返回结果为：{res.json()}")
        return res.json()

    def delete_group(self, group_id):
        url = f"/contact/v3/group/{group_id}"
        res = self.fs_requests(url=url, method="delete")
        logger.debug(f"返回结果为：{res.json()}")
        return res.json()

    def get_one_group(self, group_id):
        url = f"/contact/v3/group/{group_id}"
        res = self.fs_requests(url=url, method="get")
        logger.debug(f"查询结果为：{res.json()}")
        return res.json()

    def get_group_list(self, page_size, page_token, type_g):
        url = "/contact/v3/group/simplelist"
        param = {
            "page_size": page_size,
            "page_token": page_token,
            "type_g": type_g
        }
        res = self.fs_requests(url=url, method="get", params=param)
        logger.debug(f"查询结果为：{res.json()}")
        return res.json()


def get_test_data(file_name):
    try:
        data_path = os.path.join(BASE_PATH, 'data', file_name)
        test_data = r_data.load_yaml(data_path)
    except Exception as ex:
        pytest.skip(str(ex))
    else:
        return test_data


user_group_data = get_test_data("user_group_test_data.yaml")
